package com.atguigu.controller;

import com.atguigu.pojo.SysUser;
import com.atguigu.service.UserService;
import com.atguigu.service.impl.UserServiceImpl;
import jakarta.servlet.*;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.*;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet("/user/login")
public class UserLoginController extends HttpServlet {
    private UserService userService = new UserServiceImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            // 1.统一设置编码
            request.setCharacterEncoding("UTF-8");
            response.setCharacterEncoding("UTF-8");
            response.setContentType("text/html;charset=UTF-8");
            // 2.获取请求参数
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            // 3.请求参数传递给Service层
            SysUser sysUser = null;

                sysUser = userService.login(username, password);


            // 4.相应,如果将来login方法返回的javaBean对象不是null,证明可以登录
            if (sysUser != null) {
                request.getRequestDispatcher("/index.html").forward(request, response);
            }else {
                request.getRequestDispatcher("/login.html").forward(request, response);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}